package com.example.sample.util;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.Window;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import static android.view.View.SYSTEM_UI_FLAG_FULLSCREEN;
import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

public class SystemUIDisplayer {

    public static final int MODE_IMMERSIVE = 0;
    public static final int MODE_FULLSCREEN = 1;

    private Activity activity;
    private int mode = MODE_IMMERSIVE;
    private boolean lightStatusBar = false;
    private Integer statusBarColor = null;

    public SystemUIDisplayer(Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException();
        }
        this.activity = activity;
    }

    public SystemUIDisplayer setMode(int mode) {
        this.mode = mode;
        return this;
    }

    public SystemUIDisplayer setLightStatusBar(boolean lightStatusBar) {
        this.lightStatusBar = lightStatusBar;
        return this;
    }

    public SystemUIDisplayer setStatusBarColor(int statusBarColor) {
        this.statusBarColor = statusBarColor;
        return this;
    }

    public void display() {
        Window window = activity.getWindow();
        boolean fullScreen = mode == MODE_FULLSCREEN;
        boolean immersive = mode == MODE_IMMERSIVE;

        int sysUIFlag = 0;

        if (fullScreen) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                // 这部分代码解决androidP刘海屏问题
                sysUIFlag = SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | SYSTEM_UI_FLAG_LAYOUT_STABLE;

                window.setFlags(FLAG_TRANSLUCENT_STATUS, 0);
                window.setFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(statusBarColor != null ? statusBarColor : Color.TRANSPARENT);
            }else{
                // 无刘海及androidO刘海不用把布局扩展到顶部
                sysUIFlag = SYSTEM_UI_FLAG_FULLSCREEN;
            }
        } else if (immersive) {
            /**
             * 窗口标记部分
             */

            // 窗口标记：透明状态栏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                int windowFlag_TRANSLUCENT_STATUS;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    // android21新方法，状态栏透明需要清除窗口透明标记
                    // android23浅色状态栏也需要把它清除
                    windowFlag_TRANSLUCENT_STATUS = 0;
                } else {
                    // android19需要设置窗口透明+tint使状态栏透明
                    windowFlag_TRANSLUCENT_STATUS = FLAG_TRANSLUCENT_STATUS;
                }
                window.setFlags(windowFlag_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS);
            }

            // 窗口标记：绘制系统工具条背景
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // android21引入，其他版本不设置
                window.setFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            }

            // 状态栏颜色
            if (statusBarColor != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    window.setStatusBarColor(statusBarColor);
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    SystemBarTintManager tintManager = new SystemBarTintManager(activity);
                    tintManager.setStatusBarTintEnabled(true);
                    tintManager.setStatusBarTintColor(statusBarColor);
                }
            }

            /**
             * 系统UI标记部分
             */

            // 系统UI标记：全屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                int sysUIFlag_FullScreen = 0;
                sysUIFlag |= sysUIFlag_FullScreen;
            }

            // 系统UI标记：布局扩展到全屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                int sysUIFlag_LAYOUT_FULLSCREEN = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                sysUIFlag |= sysUIFlag_LAYOUT_FULLSCREEN;
            }

            // 系统UI标记：浅色状态栏。先处理厂商定制部分，失败了fallback到安卓M
            boolean fallback = true;
            /*if (RomHelper.isMiui()){
                *//**
                 * <a href="https://dev.mi.com/console/doc/detail?pId=1159">MIUI 9 & 10“状态栏黑色字符”实现方法变更通知</a>
                 *//*
                try {
                    if (!RomHelper.isMiuiVerGreaterThanOrEqual(2017, 7, 13)){
                        fallback = !setStatusBarContentModeMiui(window, lightStatusBar);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    fallback = true;
                }
            }else if (RomHelper.isFlyme()){
                fallback = !setStatusBarContentModeFlyme(window, lightStatusBar);
            }*/
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (fallback){
                    int sysUIFlag_LIGHT_STATUS_BAR = lightStatusBar ? SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : 0;
                    sysUIFlag |= sysUIFlag_LIGHT_STATUS_BAR;
                }
            }

            // 系统UI标记：布局固定
            sysUIFlag |= SYSTEM_UI_FLAG_LAYOUT_STABLE;
        }

        window.getDecorView().setSystemUiVisibility(sysUIFlag);
    }
}
